package com.sanjiahantang.youjiarole.module.money.views.impl;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.CircleImageView;
import com.sanjiahantang.uilibrary.views.PayPsdInputView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseApplication;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.common.beans.UserOBean;
import com.sanjiahantang.youjiarole.module.common.presenters.MeAccountPresenter;
import com.sanjiahantang.youjiarole.module.common.views.MeAccountView;
import com.sanjiahantang.youjiarole.module.me.order.beans.ShopListBean;
import com.sanjiahantang.youjiarole.module.money.beans.BankCardBean;
import com.sanjiahantang.youjiarole.utils.KeyBoardUtils;

/**
 * t提现页面
 * Created by admin on 2017/10/28.
 */

public class MeWithdrawalsActivity extends BaseMvpActivity<MeAccountView, MeAccountPresenter> implements MeAccountView, View.OnClickListener {
    private  View fl_Left, addBank;
    private  TextView title, btn_tixian, txtbankName, txtbankNo, txtMoney,txtNoBank;
    private  CircleImageView iv_bank;
    private Dialog payPasswordDialog;

    private  Dialog dialog;
    private BankCardBean.Item bankData = null;
    @Override
    public MeAccountPresenter initPresenter() {
        return new MeAccountPresenter();
    }
      private  String banktype=null;
    @Override
    public void initView() {
        setContentView(R.layout.activity_withdrawals);
        fl_Left = findViewById(R.id.fl_Left);
        fl_Left.setOnClickListener(this);
        addBank = findViewById(R.id.addBank);
        addBank.setOnClickListener(this);
        findViewById(R.id.btn_tixian).setOnClickListener(this);
        title = (TextView) findViewById(R.id.title);
        txtbankName = (TextView) findViewById(R.id.one);
        txtbankNo = (TextView) findViewById(R.id.txtbankno);
        txtMoney = (TextView) findViewById(R.id.txtmoney);
        txtNoBank = (TextView) findViewById(R.id.txtNoBank);
        presenter.CheckBankInfo($this, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                boolean flag = true;
                if (content == null){
                    flag = false;
                }else if (content instanceof BankCardBean.Card){
                    BankCardBean.Card card = (BankCardBean.Card) content;
                    if (card == null){
                        flag = false;
                    }else{
                        if (card.getData() == null || TextUtils.isEmpty(card.getData().getBankCardName())){
                            flag = false;
                        }else{
                            bankData = card.getData();
                            setBankName(card.getData().getBankGenneralName());
                            if (TextUtils.isEmpty(card.getData().getBankCardNo())){
                                txtbankNo.setVisibility(View.INVISIBLE);
                            }else{
                                setBankNo(card.getData().getBankCardNo());
                                txtbankNo.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }else{
                    flag = false;
                }

                if (flag){
                    txtbankName.setVisibility(View.VISIBLE);
                    txtNoBank.setVisibility(View.GONE);
                }else{
                    txtbankName.setVisibility(View.GONE);
                    txtbankNo.setVisibility(View.GONE);
                    txtNoBank.setVisibility(View.VISIBLE);
                }

            }
        });


        if (payPasswordDialog == null){
            payPasswordDialog = new Dialog($this,R.style.dialog_common);
            View payPasswordView = LayoutInflater.from($this).inflate(R.layout.dialog_pay_password,null);
            final PayPsdInputView passwordInputView = (PayPsdInputView) payPasswordView.findViewById(R.id.passwordView);
            payPasswordDialog.setContentView(payPasswordView);
            passwordInputView.setComparePassword( new PayPsdInputView.onPasswordListener() {

                @Override
                public void onDifference(String oldPsd, String newPsd) {

                }

                @Override
                public void onEqual(String psd) {
                }

                @Override
                public void inputFinished(final String inputPsd) {
                    passwordInputView.cleanPsd();
                    KeyBoardUtils.closeKeybordOnly($this);
                    payPasswordDialog.dismiss();
                    showProgress();
                    presenter.WithdrawalsMoney($this,txtMoney.getText().toString(), inputPsd,bankData.getBankCardNo(),new BaseView.OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            hideProgress();
                            if (content != null){
                                Utils.showToast($this,"提现成功");
                                txtMoney.setText("");
                                BaseApplication.clearActivity();
                                finish();
                            } else{
                                Utils.showToast($this,"很遗憾，提现失败了");
                            }
                        }
                    });
                }
            });
        }
    }

    @Override
    public void initData() {
        title.setText("提现");

    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.addBank:
                Intent intent = new Intent(this, MeAddBankListActivity.class);
                Bundle b = new Bundle();
             //   b.putSerializable(KEY_PARAM, ConfigValue.banklist);
                intent.putExtra(KEY_PARAM, b);
                intent.putExtra(KEY_TITLE, "我的银行卡");
                startActivityForResult(intent, BANK_LIST);

                break;
            case R.id.btn_tixian:
                if (txtMoney.getText().toString().equals("")) {
                    Utils.showToast(this, "输入金额不能为空！");
                } else {
                    payPasswordDialog.show();
                }

                break;

        }

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode != Activity.RESULT_OK) {
            switch (requestCode) {
                case BANK_LIST:
                    if (data != null) {
                        bankData = (BankCardBean.Item) data.getSerializableExtra(KEY_PARAM);
                        if (bankData != null) {
                            setBankName(bankData.getBankGenneralName());
                            setBankNo(bankData.getBankCardNo());
                        }
                    }
                    break;
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void ChoicePayMethod() {
        dialog = new Dialog(this, R.style.Dialog_Fullscreen);
        dialog.setContentView(R.layout.dialog_payall);
        TextView txtNeedPrice = (TextView) dialog.findViewById(R.id.txtNeedPrice);
        final RadioButton rbWX = (RadioButton) dialog.findViewById(R.id.rbWX);
        final RadioButton rbZFB = (RadioButton) dialog.findViewById(R.id.rbZFB);
        final RadioButton rbCFT = (RadioButton) dialog.findViewById(R.id.rbCFT);
        final RadioButton rbYHK = (RadioButton) dialog.findViewById(R.id.rbYHK);
        final RelativeLayout vWX = (RelativeLayout) dialog.findViewById(R.id.vWX);
        final RelativeLayout vZFB = (RelativeLayout) dialog.findViewById(R.id.vZFB);
        final RelativeLayout vCFT = (RelativeLayout) dialog.findViewById(R.id.vCFT);
        final RelativeLayout vYHK = (RelativeLayout) dialog.findViewById(R.id.vYHK);
        txtNeedPrice.setText(txtMoney.getText().toString());
        dialog.findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //请求微信、支付宝、银行卡、财付通、成功后返回的结果成功：1或者失败：2
                String  state="1";
//             presenter.WithdrawalsMoney($this, txtMoney.getText().toString(), banktype, txtbankNo.getText().toString(),state);
            }
        });

        vWX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rbWX.setEnabled(false);
                rbCFT.setEnabled(false);
                rbZFB.setEnabled(false);
                rbYHK.setEnabled(false);

                rbCFT.setChecked(false);
                rbZFB.setChecked(false);
                rbWX.setChecked(true);
                rbYHK.setChecked(false);
                banktype="3";
                Utils.showToast($this, "微信"+banktype);
            }
        });
        vYHK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rbWX.setEnabled(false);
                rbCFT.setEnabled(false);
                rbZFB.setEnabled(false);
                rbYHK.setEnabled(false);

                rbCFT.setChecked(false);
                rbZFB.setChecked(false);
                rbWX.setChecked(false);
                rbYHK.setChecked(true);

                banktype="5";
                Utils.showToast($this, "银行卡"+banktype);
            }
        });
        vZFB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rbWX.setEnabled(false);
                rbCFT.setEnabled(false);
                rbZFB.setEnabled(false);
                rbYHK.setEnabled(false);

                rbCFT.setChecked(false);
                rbZFB.setChecked(true);
                rbWX.setChecked(false);
                rbYHK.setChecked(false);
                banktype="2";
                Utils.showToast($this, "支付宝 "+banktype);
            }
        });
        vCFT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rbWX.setEnabled(false);
                rbCFT.setEnabled(false);
                rbZFB.setEnabled(false);
                rbYHK.setEnabled(false);

                rbCFT.setChecked(true);
                rbZFB.setChecked(false);
                rbWX.setChecked(false);
                rbYHK.setChecked(false);

                banktype="4";
                Utils.showToast($this, "财付通 "+banktype);
            }
        });

        dialog.show();
    }

    public void setBankName(String bankName) {
        txtbankName.setText(bankName);
    }


    public void setBankNo(String bankNo) {
        txtbankNo.setText(String.format("尾号%s", Utils.getLast4(bankNo)));
    }
}
